<?php

    namespace yaliSDK\common;
    class Entity {
        public function __toString() {
            return $this->toJson();
        }

        public function toJson($options = JSON_UNESCAPED_UNICODE) {
            $d = get_object_vars($this);
            return $this->__toJson($d, $options);
        }

        protected function __toJson($d, $options) {

            $array = [];
            foreach ($d as $k => $item) {
                switch (gettype($item)) {
                    case 'object':
                        $array[$k] = json_decode($item->__toString());
                        break;
                    case 'array':
                        $array[$k] = json_decode($this->__toJson($item, $options));
                        break;
                    default:
                        $array[$k] = $item;
                        break;
                }
            }
            return json_encode($array, $options);
        }
    }